package com.example.myapplication.MainFragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.HomeAdapter.HomeAdapter;
import com.example.myapplication.HomeAdapter.HomeGridViewAdapter;
import com.example.myapplication.HomeAdapter.HomeListViewAdapter;
import com.example.myapplication.HomeAdapter.HomeXinWenAdapter;
import com.example.myapplication.HomeBean.HomeGridViewBean;
import com.example.myapplication.HomeBean.HomeListViewBean;
import com.example.myapplication.HomeBean.HomeXinWenBean;
import com.example.myapplication.HomeRecyclerViewFragment.JingPinBanJi;
import com.example.myapplication.HomeRecyclerViewFragment.JingPinKeCheng;
import com.example.myapplication.HomeRecyclerViewFragment.XianShiZheKou;
import com.example.myapplication.Activity.MainActivity;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class FragmentHome extends Fragment{
    ViewPager Home_LunBo;
    List<View> list=new ArrayList<>();
    View LunBo1,LunBo2,LunBo3;
    HomeAdapter homeAdapter;
    GridView Home_gv;
    List<HomeGridViewBean> Home_gv_list=new ArrayList<>();
    HomeGridViewAdapter homeGridViewAdapter;
    RadioGroup Home_rg;
    RadioButton Home_rb1,Home_rb2,Home_rb3,Home_rb4;
    //fragmet事务类
    FragmentManager Home_fm;
    //fragmet管理类
    FragmentTransaction Home_ft;
    View Home_rb_view1,Home_rb_view2,Home_rb_view3;
    ListView Home_lv;
    List<HomeListViewBean> Home_lv_list=new ArrayList<>();
    ScrollView scrollView;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {

            super.handleMessage(msg);
            if(msg.what==1){
                //实现轮播
                Home_LunBo.setCurrentItem((Home_LunBo.getCurrentItem()+1)%3);
                NoYuanDianSelect();
                switch (Home_LunBo.getCurrentItem()){
                    case 0:
                        LunBo1.setSelected(true);
                        break;
                    case 1:
                        LunBo2.setSelected(true);
                        break;
                    case 2:
                        LunBo3.setSelected(true);
                        break;
                }
            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_home,container,false);
        //初始化事务
        Home_fm= getActivity().getSupportFragmentManager();
        //开启事务
        Home_ft=Home_fm.beginTransaction();
        //设定选中
        Home_ft.replace(R.id.Home_fl,new JingPinKeCheng());
        //提交事务
        Home_ft.commit();
        initView(view);
        //轮播
        HomeLunBo();
        Home_rb_view1.setSelected(true);
        Home_rb1.setTextSize(15);
        return view;
    }

    private void HomeLunBo() {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (!Thread.interrupted()){
                        Thread.sleep(2000);
                        handler.sendEmptyMessage(1);
                    }
                }catch (InterruptedException e){
                    throw new RuntimeException(e);
                }

            }
        });
        thread.start();
    }

    private void initView(View view) {
        //绑定控件
        Home_LunBo=view.findViewById(R.id.Home_LunBo);

        LunBo1=view.findViewById(R.id.Home_LunBo_view1);
        LunBo2=view.findViewById(R.id.Home_LunBo_view2);
        LunBo3=view.findViewById(R.id.Home_LunBo_view3);
        //添加tab点击事件
        FragmentTabSelect(view);
        //ListView
        HomeLisView(view);
        HomeXinWen(view);
        //添加gridview数据
        Home_gv_list.add(new HomeGridViewBean(R.mipmap.homehuopao,"火炮"));
        Home_gv_list.add(new HomeGridViewBean(R.mipmap.homewurenji,"无人机"));
        Home_gv_list.add(new HomeGridViewBean(R.mipmap.homedaodan,"导弹"));
        Home_gv_list.add(new HomeGridViewBean(R.mipmap.homeqianting,"潜艇"));

        //设置圆点
        NoYuanDianSelect();
        LunBo1.setSelected(true);
        //绑定轮播图中的图片
        View Home_LunB0_img1=LayoutInflater.from(getActivity()).inflate(R.layout.home_lunbo_img1,null);
        View Home_LunB0_img2=LayoutInflater.from(getActivity()).inflate(R.layout.home_lunbo_img2,null);
        View Home_LunB0_img3=LayoutInflater.from(getActivity()).inflate(R.layout.home_lunbo_img3,null);
        list.add(Home_LunB0_img1);
        list.add(Home_LunB0_img2);
        list.add(Home_LunB0_img3);

        //初始化适配器
        homeAdapter=new HomeAdapter(list);
        homeGridViewAdapter=new HomeGridViewAdapter(Home_gv_list,getActivity(),getContext());
        //绑定适配器
        Home_LunBo.setAdapter(homeAdapter);
        Home_gv.setAdapter(homeGridViewAdapter);
        //设定第几张图片
        Home_LunBo.setCurrentItem(0);


    }

    private void HomeXinWen(View view) {
        ListView HomeXinWen=view.findViewById(R.id.Home_XinWen);
        List<HomeXinWenBean>listxinWenBean=new ArrayList<>();
        listxinWenBean.add(new HomeXinWenBean("1","卡霍夫卡水电站大坝被毁！！！"));
        listxinWenBean.add(new HomeXinWenBean("2","扎总司令和他的四位女下属..."));
        listxinWenBean.add(new HomeXinWenBean("3","乌军春季攻势何时开始？"));
        listxinWenBean.add(new HomeXinWenBean("4","大毛要撑不住了？竟有东秘在背后出手？"));
        HomeXinWenAdapter homeXinWenAdapter=new HomeXinWenAdapter(listxinWenBean,getActivity());
        HomeXinWen.setAdapter(homeXinWenAdapter);

    }

    private void HomeLisView(View view) {
        Home_lv=view.findViewById(R.id.Home_lv);
        scrollView=view.findViewById(R.id.Home_sv);
        Home_lv_list.add(new HomeListViewBean(R.mipmap.look,R.mipmap.meiguoguoqi,"美国",10,"人游览"));
        Home_lv_list.add(new HomeListViewBean(R.mipmap.look,R.mipmap.eluosiguoqi,"俄罗斯",10,"人游览"));
        Home_lv_list.add(new HomeListViewBean(R.mipmap.look,R.mipmap.chinaguoqi,"中国",10,"人游览"));
        Home_lv_list.add(new HomeListViewBean(R.mipmap.look,R.mipmap.yinduguoqi,"印度",10,"人游览"));
        Home_lv_list.add(new HomeListViewBean(R.mipmap.look,R.mipmap.yingguoguoqi,"英国",10,"人游览"));
        Home_lv_list.add(new HomeListViewBean(R.mipmap.look,R.mipmap.hanguoguoqi,"韩国",10,"人游览"));
        Home_lv_list.add(new HomeListViewBean(R.mipmap.look,R.mipmap.bajisitanguoqi,"巴基斯坦",10,"人游览"));
        Home_lv_list.add(new HomeListViewBean(R.mipmap.look,R.mipmap.ribenguoqi,"日本",10,"人游览"));
        Home_lv_list.add(new HomeListViewBean(R.mipmap.look,R.mipmap.faguoguoqi,"法国",10,"人游览"));
        Home_lv_list.add(new HomeListViewBean(R.mipmap.look,R.mipmap.yidaliguoqi,"意大利",10,"人游览"));

        HomeListViewAdapter homeListViewAdapter=new HomeListViewAdapter(Home_lv_list,getActivity());
        Home_lv.setAdapter(homeListViewAdapter);

        //处理滑动冲突问题
        Home_lv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                //scrollView.getScrollY()超出屏幕的高度
                //scrollView.getHeight()屏幕显示的高度
                //scrollView.getPaddingTop()，scrollView.getPaddingBottom()//上下Padding
                //scrollView.getChildAt(0).getHeight()//scrollView唯一子View的高度
                if(scrollView.getScrollY() + scrollView.getHeight() - scrollView.getPaddingTop() - scrollView.getPaddingBottom() == scrollView.getChildAt(0).getHeight()){
                    scrollView.requestDisallowInterceptTouchEvent(true);
                }
                return false;
            }
        });


    }

    private void FragmentTabSelect(View view) {
        Home_gv=view.findViewById(R.id.Home_gv);

        Home_rg=view.findViewById(R.id.Home_rb);
        Home_rb1=view.findViewById(R.id.Home_rb1);
        Home_rb2=view.findViewById(R.id.Home_rb2);
        Home_rb3=view.findViewById(R.id.Home_rb3);
        Home_rb4=view.findViewById(R.id.Home_rb4);

        Home_rb_view1=view.findViewById(R.id.Home_View1);
        Home_rb_view2=view.findViewById(R.id.Home_View2);
        Home_rb_view3=view.findViewById(R.id.Home_View3);

        Home_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                //设置未选中状态
                NoRBSelect();
                //继续开启事务
                Home_ft=Home_fm.beginTransaction();
                switch (i){
                    case R.id.Home_rb1:
                        Home_ft.replace(R.id.Home_fl,new JingPinKeCheng());
                        Home_rb_view1.setSelected(true);
                        Home_rb1.setTextSize(15);
                        break;
                    case R.id.Home_rb2:
                        Home_ft.replace(R.id.Home_fl,new JingPinBanJi());
                        Home_rb_view2.setSelected(true);
                        Home_rb2.setTextSize(15);
                        break;
                    case R.id.Home_rb3:
                        Home_ft.replace(R.id.Home_fl,new XianShiZheKou());
                        Home_rb_view3.setSelected(true);
                        Home_rb3.setTextSize(15);
                        break;
                    case R.id.Home_rb4:

//                        Intent intent=new Intent(getActivity(), MainActivity.class);
//                        intent.putExtra("num",1);
//                        startActivity(intent);
                        break;
                }
                Home_ft.commit();
            }
        });

    }


    private void NoRBSelect() {
        Home_rb_view1.setSelected(false);
        Home_rb_view2.setSelected(false);
        Home_rb_view3.setSelected(false);
        Home_rb1.setTextSize(10);
        Home_rb2.setTextSize(10);
        Home_rb3.setTextSize(10);
    }

    private void NoYuanDianSelect() {
        LunBo1.setSelected(false);
        LunBo2.setSelected(false);
        LunBo3.setSelected(false);
    }

}
